package com.linzm.leetcode.primary.双指针;

import org.junit.Test;

import java.sql.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author zimingl
 * @Date 2022/10/23 23:50
 * @Description: TODO
 */
public class 双指针830较大分组的位置 {
    public List<List<Integer>> largeGroupPositions(String s) {
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        char[] chars = s.toCharArray();
        int slowIndex = 0;
        int count = 0;
        for (int fastIndex = 1; fastIndex < chars.length; fastIndex++) {
            if (chars[fastIndex] == chars[slowIndex]){
                count ++;
            }
            if (chars[fastIndex] != chars[slowIndex] && count <= 1){
                count = 0;
            }
            // "sss"
            if (fastIndex == chars.length - 1 && chars[fastIndex] == chars[slowIndex] && count > 1){
                ArrayList<Integer> integers = new ArrayList<>();
                addElement(integers, fastIndex - count, fastIndex);
                list.add(integers);
                count = 0;
            }
            if (chars[fastIndex] != chars[slowIndex] && count > 1){
                ArrayList<Integer> integers = new ArrayList<>();
                addElement(integers, fastIndex-count-1, fastIndex-1);
                list.add(integers);
                count = 0;
            }
            slowIndex ++;
        }
        return list;
    }

    public void addElement(List<Integer> integers, Integer element1, Integer element2){
        integers.add(element1);
        integers.add(element2);
    }

    @Test
    public void test(){
        String s = "abcdddeeeeaabbbcd";
        String s2 = "bababbaaab";
        List<List<Integer>> lists = largeGroupPositions(s2);
        lists.forEach(System.out::println);
    }
}
